/* Allegro */
#include <allegro.h>

/* Standard includes */
#include <stdio.h>

/* Own includes */
#include "../include/mainmenu.h"
#include "../include/maingame.h"
#include "../include/sceneloader.h"
#include "../include/charloader.h"
#include "../include/input.h"
#include "../include/output.h"

/* Internal prototypes */
int game_load();
void game_cleanup();
void game_draw();
void game_timer();

void game_run()
{
    int ntick; /* number of ticks (Timer) */

    /* Clear the screen so the menu doesent show */
    cls(1);

    /* Present a bogus loadingscreen */
    text_center(buffer->w / 2, buffer->h / 2, 255, 0, 0, "Loading");
    sync();

    /* Do the loading routine */
    if(game_load() != 0)
        return;

    /* The main game loop */
    while(!key[KEY_ESC])
    {
        ntick = ticks;
        ticks = 0;


        /* Do logics */
        for(; ntick > 0; ntick--) {
            indy->state=0;
            input();
            game_timer();
        }

        /* Draw graphics every loop */
        cls(0);
        game_draw();
        sync();
    }

    /* Cleanup */
    game_cleanup();
}

int game_load()
{
    /* Load the scene */
    if(scene_load("001") != 0)
    {
        printf("ERROR: Could not load scene.\nTerminating.\n");
        mm_done = 1;
        return 1;
    }

    /* Load the character */
    if(char_load("001") != 0)
    {
        printf("ERROR: Could not load character.\nTerminating.\n");
        mm_done = 1;
        return 1;
    }

    return 0;
}

void game_draw()
{
    scene_draw();
    char_animation();
    if(debugmode == 1) debuginfo(2, 2);
}

void game_timer()
{
    char_timer(7);
}

void game_cleanup()
{
    char_cleanup();
    scene_cleanup();
}
